#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <fstream>

int main() 
{ 
	const char * filename = "Liang.txt";
	int filesize = 1024;
	{  //Create a file
		boost::interprocess::file_mapping::remove(filename);
		std::filebuf fbuf;
		fbuf.open(filename, std::ios_base::in | std::ios_base::out
			| std::ios_base::trunc | std::ios_base::binary);
		//Set the size
		fbuf.pubseekoff(filesize-1, std::ios_base::beg);
		fbuf.sputc(0);
	}
	try {
		boost::interprocess::file_mapping m_file(filename,boost::interprocess::read_write);
		std::cout << "file name: " << m_file.get_name() << std::endl;
		boost::interprocess::offset_t size = filesize;

		boost::interprocess::mapped_region region(m_file, boost::interprocess::read_write); 
		std::cout << std::hex << "0x" << region.get_address() << std::endl; 
		std::cout << std::dec << region.get_size() << std::endl; 
		int *i1 = static_cast<int*>(region.get_address());
		*i1 = 1212;
		region.flush();

		boost::interprocess::mapped_region region2(m_file, boost::interprocess::read_only, 0,size/2 ); 
		std::cout << std::hex << "0x" << region2.get_address() << std::endl; 
		std::cout << std::dec << region2.get_size() << std::endl;
		int *i2 = static_cast<int*>(region2.get_address());
		std::cout << *i2 << std::endl;
		region2.flush(0,size/2);

		std::size_t page_size = boost::interprocess::mapped_region::get_page_size();
		std::cout << "page size: " << page_size << std::endl;
	}
	catch (boost::interprocess::interprocess_exception &ex) 
	{ 
		std::cerr << ex.what() << std::endl; 
	} 


	return 0;
} 